package org.imaginedays.androidall.app;

import org.imaginedays.androidall.utils.DeviceInfoUtils;
import org.imaginedays.androidall.utils.crash.CrashHandler;
import org.imaginedays.androidall.utils.root_check.CoreTask;

import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class AndroidAllApp extends Application {

	public static String TAG = "AndroidAllApp";

	public static AndroidAllApp instance;

	public CoreTask coreTask = null;
	
	private final static float HEAP_UTILIZATION = 0.75f;  
	private final static int MIN_HEAP_SIZE = 6* 1024* 1024 ;  

	public static AndroidAllApp getInstance() {
		return instance;
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		instance = this;

		// 注册全局错误日志捕捉
		CrashHandler.getInstance().init(getApplicationContext());
		
		// VMRuntime.getRuntime().setTargetHeapUtilization(HEAP_UTILIZATION);
		//  VMRuntime.getRuntime().setMinimumHeapSize(MIN_HEAP_SIZE);   

		DeviceInfoUtils diu = new DeviceInfoUtils(this);
		diu.setDeviceScreenInfo();

		// 检查device root
		coreTask = new CoreTask();
		coreTask.setPath(this.getApplicationContext().getFilesDir().getParent());
		Log.d(TAG, "Current directory is "
				+ this.getApplicationContext().getFilesDir().getParent());

		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getApplicationContext())
				.threadPoolSize(3)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.memoryCacheSize(1500000)
				// 1.5 Mb
				.denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator())
				.enableLogging() // Not necessary in common
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);

	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

}
